﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using CommonLib.Pattern.FSM;
using CommonLib.Util;

public class EnemyStateTransitionBase : FSMTransitionBase
{
	protected RoleCtrl Role { get; set; }

	public EnemyStateTransitionBase(string _key, IState _from, IState _to, int _priority, RoleCtrl _role) : base(_key, _from, _to, _priority)
	{
		Role = _role;
	}


	protected TransitionParam GetTransistionParam(TransitionParamType type) //=> (FromState.Machine as FSMEnemy).Levles[Role.RoleEntity.CurrentStage].Params[type];
	{
		int stage = Role.RoleEntity.CurrentStage;
		FSMEnemy fSMEnemy = FromState.Machine as FSMEnemy;
		//Check stage valid
		if (stage < 0)
			return null;
		if (stage >= fSMEnemy.Levles.Count)
			stage = fSMEnemy.Levles.Count - 1;
		TransitionParam tParams = fSMEnemy.Levles[stage].Params[type];
		return tParams;
	}

	protected bool FindNearestDangrous(float vigilance, out Collider2D collider)
	{
		collider = null;
		if (Collider2DUtil.Box2DFindNearest(Role.gameObject.transform.position,
			new Vector2(Role.SubManagerMove.Collider.bounds.size.x + 2 * vigilance / 100.0f, Role.SubManagerMove.Collider.bounds.size.y + 2 * vigilance / 100.0f),
			(col) =>
			{
				if ((col.gameObject.tag == Const.Tag_Player && col.gameObject != Role.gameObject))
				{
					 RoleCtrl temp = col.GetComponent<RoleCtrl>();
					 return temp.RoleEntity.CurrentScore > Role.RoleEntity.CurrentScore ? true : false;
				}
				else if (col.gameObject.tag == Const.Tag_ItemEffect)
				{
					 EffectItemBase effectItem = col.GetComponent<EffectItemBase>();
					 return !effectItem.IsPositive;
				}
				else
					return false;
			}, 
			out Collider2D dangrous) == true)
		{
			collider = dangrous;
			return true;
		}
		else
			return false;

	}
	protected bool FindNearestChase(float vigilance,out Collider2D collider)
	{
		collider = null;
		if (Collider2DUtil.Box2DFindNearest(Role.gameObject.transform.position,
			new Vector2(Role.SubManagerMove.Collider.bounds.size.x + 2 * vigilance / 100.0f, Role.SubManagerMove.Collider.bounds.size.y + 2 * vigilance / 100.0f),
			(col) =>
			{
				if (col.gameObject.tag == Const.Tag_Player && col.gameObject != Role.gameObject)
				{
					 RoleCtrl temp = col.GetComponent<RoleCtrl>();
					return temp.RoleEntity.CurrentScore < Role.RoleEntity.CurrentScore ? true : false;
				}
				else
					return false;
			},
			out Collider2D target) == true)
		{
			collider = target;
			return true;
		}
		else
			return false;
	}
}
